package com.ebupt.migu.music.messageCenter.service.impl;

import com.ebupt.migu.music.common.constants.RoleConstants;
import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.util.DateUtil;
import com.ebupt.migu.music.messageCenter.mapper.MessageCenterMapper;
import com.ebupt.migu.music.messageCenter.service.MessageCenterService;
import com.ebupt.migu.music.system.mapper.RoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Intellij IDEA.
 * User:  liuyangyang@cd.ebupt.com
 * Date:  2020/3/9
 */
@Slf4j
@Service
public class MessageCenterServiceImpl implements MessageCenterService {

    @Autowired
    private MessageCenterMapper messageCenterMapper;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 创建消息通知类型
     *
     * @param params
     * @return
     */
    @Override
    public ResultObject addNoticeType(Map<String, Object> params) {
        ResultObject res = new ResultObject();
        Date date = new Date();
        // 生成通知ID
        String typeId = "NS" + DateUtil.parseDateToStr(date, DateUtil.DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        params.put("typeId", typeId);
        // 发送者名称【不可重复】
        String nickName = params.get("nickName").toString();
        Map<String, Object> isNickNameMap = new HashMap<>();
        isNickNameMap.put("nickName", nickName);
        boolean isNameExist = messageCenterMapper.isNickNameExist(isNickNameMap);
        if (isNameExist) {
            res.setCode(StatusEnum.NICK_NAME_EXIST.getCode());
            res.setMsg(StatusEnum.NICK_NAME_EXIST.getMsg());
            return res;
        }
        // 保存消息通知
        messageCenterMapper.addNoticeType(params);
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        return res;
    }

    /**
     * 更新消息通知类型
     *
     * @param params
     * @return
     */
    @Override
    public ResultObject updateNoticeType(Map<String, Object> params) {
        ResultObject res = new ResultObject();
        String nickName = params.get("nickName").toString();
        int id = Integer.parseInt(params.get("id").toString());
        Map<String, Object> nickNameMap = new HashMap<>();
        nickNameMap.put("nickName", nickName);
        nickNameMap.put("id", id);
        boolean isNameExist = messageCenterMapper.isNickNameExistExceptID(nickNameMap);
        if (isNameExist) {
            res.setCode(StatusEnum.NICK_NAME_EXIST.getCode());
            res.setMsg(StatusEnum.NICK_NAME_EXIST.getMsg());
            return res;
        }
        messageCenterMapper.updateNoticeType(params);
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        return res;
    }

    /**
     * 查询通知类型列表
     *
     * @param params
     * @return
     */
    @Override
    public ResultObject queryNoticeList(Map<String, Object> params) {
        ResultObject<Page<HashMap<String, Object>>> res = new ResultObject<>();
        try {
            // 超级管理员可以查询所有运营策略， 非超级管理员仅能查询本部门运营策略
//            String userID = params.get("userID").toString();
//            List<String> roles = roleMapper.queryRoleIdByUserId(userID);
//            if (roles.contains(RoleConstants.SUPER_ADMIN)) {
            // 如果是超级管理员，查询范围为全部可见
            params.remove("creatorDepartment");
//            }
            // 总数
            Integer total = messageCenterMapper.queryNoticeCount(params);
            // 分页查询结果集
            List<HashMap<String, Object>> datas = messageCenterMapper.queryNoticeList(params);
            // 分页
            Page<HashMap<String, Object>> page = new Page<>();
            page.setTotal(total);
            page.setData(datas);
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            res.setData(page);
            return res;
        } catch (Exception e) {
            log.error("查询策略列表失败", e);
            throw new MyException(StatusEnum.ERROR, "查询策略列表失败");
        }

    }

}
